﻿function Storage(type) {
    if (type) {
        switch (type) {
            case "local":
                this.storage = window.localStorage;
                break;
            case "session":
                this.storage = window.sessionStorage;
                break;
        }
    }
    if (!this.storage) {
        throw "This browser does not support " + type + "Storage";
    }
}
Storage.prototype = {
    storage: window.localStorage,
    length: function () {
        return this.storage.length;
    },
    getName: function (i) {
        return this.storage.key(i);
    },
    setItem: function (name, value) {
        this.removeItem(name);
        this.storage.setItem(name, value);
    },
    getItem: function (name) {
        return this.storage.getItem(name);
    },
    setJsonItem: function (name, value) {
        this.removeItem(name);
        this.storage.setItem(name, JSON.stringify(value));
    },
    getJsonItem: function () {
        return JSON.parse(this.storage.getItem(name));
    },
    removeItem: function (name) {
        this.storage.removeItem(name);
    },
    clear: function () {
        this.storage.clear();
    },
    getAllValue: function () {
        var storages = new Array();
        for (var i = 0, length = this.storage.length; i < length; i++) {
            storages.push(this.getItem(this.getName(i)));
        }
        return storages;
    },
    addChangeEvent: function (handle_storage) {
        if (window.addEventListener) {
            window.addEventListener("storage", handle_storage, false);
        } else if (window.attachEvent) {
            window.attachEvent("onstorage", handle_storage);
        }
    }
}